package com.wg.web_app.controller;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.wg.web_app.service.GroupService;

@SpringBootApplication
@Controller
public class GroupController {
	@RequestMapping(value = "/create_group", method = RequestMethod.POST)
	@ResponseBody
	public String createGroup(@RequestParam("userid") int userid, @RequestParam("name") String name,
			@RequestParam("detail") String detail) {
		GroupService group = new GroupService();
		return new Gson().toJson(group.create(userid,name,detail));
	}

	@RequestMapping(value = "/apply_group", method = RequestMethod.POST)
	@ResponseBody
	public String applyGroups(@RequestParam("userid") int userId, @RequestParam("groupid") int groupId) {
		GroupService group = new GroupService();
		return new Gson().toJson(group.apply(userId,groupId));
	}

	@RequestMapping(value = "/pass_applicant", method = RequestMethod.PUT)
	@ResponseBody
	public String passApplicant(@RequestParam("userid") int userId, @RequestParam("groupid") int groupId) {
		GroupService group = new GroupService();
		return new Gson().toJson(group.pass(userId,groupId));
	}

	@RequestMapping(value = "/get_managing", method = RequestMethod.GET)
	@ResponseBody
	public String getManaging(@RequestParam("userid") int userId) {
		GroupService group = new GroupService();
		return new Gson().toJson(group.getManaging(userId));
	}

	@RequestMapping(value = "/set_mananger", method = RequestMethod.PUT)
	@ResponseBody
	public String setMananger(@RequestParam("userid") int userId, @RequestParam("groupid") int groupId) {
		GroupService group = new GroupService();
		return new Gson().toJson(group.set_mananger(userId,groupId));
	}

	@RequestMapping(value = "/get_applicant", method = RequestMethod.GET)
	@ResponseBody
	public String getApplicant(@RequestParam("groupid") int groupId) {
		GroupService group = new GroupService();
		return new Gson().toJson(group.getApplicant(groupId));
	}
	
	@RequestMapping(value = "/search_gourp", method = RequestMethod.GET)
	@ResponseBody
	public String searchGroup(@RequestParam("group_name") String name) {
		GroupService group = new GroupService();
		return new Gson().toJson(group.search(name));
	}

	@RequestMapping(value = "/group_detail", method = RequestMethod.GET)
	@ResponseBody
	public String groupDetail(@RequestParam("groupid") int groupId) {
		GroupService group = new GroupService();
		return new Gson().toJson(group.detail(groupId));
	}
}
